<template>
	<view class="detail-box u-skeleton">
		<u-navbar :title="bookDetail.name" :border-bottom="false" :background="navBarBackground" :back-icon-color="navBarColor"
		 :title-color="navBarTextColor">
		</u-navbar>

		<view class="detail-bg" :style="'background-image:url('+ bookDetail.image +')'"></view>
		<view class="detail-wrap"></view>

		<view class="detail-content">
			<view class="detail-top">
				<view class="detail-left">
					<view class="detail-main u-skeleton-rect">
						<view class="detail-name">{{bookDetail.name}}</view>
						<view class="detail-info">
							<view class="detail-writer">{{bookDetail.writer}}</view>
							<text class="detail-over">{{bookDetail.is_over || '未知' }} | {{bookDetail.type || '未知'}}</text>
						</view>
					</view>
					<view class="detail-new  u-skeleton-rect" @click="handleToNew">
						<text class="u-line-1">{{bookDetail.new_chapter||'暂无更新章节'}}</text>
						<u-icon name="arrow-right" color="#fff" size="28"></u-icon>
					</view>
				</view>
				<u-image class="u-skeleton-fillet" width="144rpx" height="194rpx" border-radius="16" :src="bookDetail.image" mode="cover"
				 :fade="true" duration="450">
					<u-loading slot="loading"></u-loading>
				</u-image>
			</view>
			<view class="detail-intro  u-skeleton-rect">
				<view>简介：</view>
				<view class="">{{bookDetail.intro || '暂无简介'}}</view>
			</view>
		</view>

		<view class="detail-list">
			<view class="list-nav u-skeleton-rect">
				<u-icon :name="topFlag?'arrow-up':'arrow-down'" color="#061B41" size="28" @click="handleToTop"></u-icon>
				<text>目录</text>
				<text @click="handleToReverse">{{reverseFlag ? '正序':'倒序'}}</text>
			</view>
			<scroll-view class="list-content" scroll-y show-scrollbar @scrolltolower="loadMoreChapter" :scroll-top="chapterListScrollTop"
			 @scroll="scroll">
				<view :class="lastReadChapterIndex == item.index ? 'active-item' : lastReadChapterIndex > item.index ? 'read-item':'list-item'"
				 v-for="item in chapterList" :key="item.index" @click="handleToCurrentChapter(item.chapter_id)">
					<view class="u-line-1" style="width: 90%;">{{item.title}}</view>
					<u-icon v-if="lastReadChapterIndex == item.index" name="play-circle-fill" color="#92C8FF" size="36" style="position: absolute;right: 0;width: 10%;"></u-icon>
					<u-icon v-if="lastReadChapterIndex > item.index" name="checkmark-circle-fill" color="rgba(6,27,65,0.75)" size="34"
					 style="position: absolute;left: -52rpx;width: 10%;"></u-icon>
				</view>
				<u-loadmore :status="status" margin-top="20" margin-bottom="20" :load-text="loadText" />
			</scroll-view>
		</view>

		<view class="detail-bottom u-skeleton-rect">
			<text class="add-shelf" @click="handleAddBookShelf">{{isInShelf?'撤出书架':'加入书架'}}</text>
			<text class="read-start" @click="handleToRead">{{ isRead? '继续阅读' : '开始阅读' }}</text>
		</view>
		<u-skeleton :loading="skeLoading" :animation="true" bg-color="#F5F8FC" :border-radius="40"></u-skeleton>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		getChapterDetail,
		getChapterList
	} from "@/api/chapter";
	import {
		mapState
	} from 'vuex'
	import _ from 'lodash'
	export default {
		name: 'detail',
		data() {
			return {
				topFlag: true,
				reverseFlag: true,
				bookDetail: {
					name: ''
				},
				chapterList: [],
				chapterCount: 0,
				navBarBackground: {
					'background': 'rgba(255,255,255,0)'
				},
				navBarColor: 'rgba(255,255,255,0.65)',
				navBarTextColor: 'rgba(0,0,0,0)',
				currentPage: 1,
				pageSize: 50,
				lastPage: 10,
				status: 'loadMore',
				orderBy: 1,
				chapterListScrollTop: 0,
				old: {
					chapterListScrollTop: 0
				},
				lastReadChapterId: 0,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				skeLoading: true
			}
		},
		computed: {
			...mapState({
				token: state => state.global.token,
				shelfList: state => state.shelf.shelfList
			}),
			isInShelf() {
				return this.shelfList.some(item => {
					return item.book_id == this.bookId
				})
			},
			lastReadChapterIndex(){
				if (this.isInShelf) {
					return this.shelfList.find(item => {
						return item.book_id == this.bookId
					}).index
				}
				return 1
			},
			isRead() {
				if (this.isInShelf) {
					return this.shelfList.find(item => {
						return item.book_id == this.bookId
					}).index != 0
				}
				return false
			}
		},
		onLoad(option) {
			this.bookId = option.bookId;
			this.init(this.bookId);
		},
		onPageScroll(e) {
			if (e.scrollTop === 0) {
				this.navBarBackground = {
					'background': 'rgba(255,255,255,0)'
				};
				this.navBarColor = 'rgba(255,255,255,0.65)';
				this.navBarTextColor = 'rgba(0,0,0,0)';
				this.topFlag = true
			} else {
				if (e.scrollTop < 100) {
					this.navBarBackground = {
						'background': `rgba(255,255,255,${e.scrollTop / 100})`
					};
					this.navBarColor = `rgba(0,0,0,${e.scrollTop / 100})`;
					this.navBarTextColor = `rgba(0,0,0,${e.scrollTop / 100})`;
				} else {
					this.navBarBackground = {
						"background": "rgba(255,255,255,1)"
					};
					this.navBarColor = 'rgba(0,0,0,1)';
					this.navBarTextColor = 'rgba(0,0,0,1)';
					this.topFlag = false;
				}
			}
		},
		onReachBottom() {
			this.topFlag = false;
		},
		methods: {
			async init(bookId) {
				if (this.token) {
					await this.$store.dispatch('updateShelfList')
				}
				const {
					data
				} = await getChapterDetail({
					bookId
				})
				this.bookDetail = data;
				await this.getChapterList(bookId);
				this.skeLoading = false;
			},
			// 获取章节列表（分页）
			getChapterList(bookId) {
				this.status = 'loading'
				if (this.currentPage == this.lastPage) {
					this.status = 'nomore'
					return
				}
				return new Promise((resolve, reject) => {
					getChapterList({
						bookId: this.bookId,
						page: this.currentPage,
						pageSize: this.pageSize,
						orderBy: this.orderBy
					}).then((res) => {
						this.chapterList = this.chapterList.concat(res.data.list)
						if (res.data.total == 0) {
							this.$refs.uToast.show({
								title: '未找到章节列表',
								type: 'error',
								position: 'top'
							});
							this.status = 'loadmore'
						} else {
							this.currentPage += 1;
							this.chapterCount = res.data.total;
							this.lastPage = res.data.totalPage;
						}
						resolve("ok");
					});
				});
			},
			// 章节加载
			loadMoreChapter: _.throttle(function() {
				this.getChapterList()
			}, 500),
			// 滚动
			scroll(e) {
				this.old.chapterListScrollTop = e.detail.scrollTop
			},
			// 排序方式
			handleToReverse: _.debounce(function() {
				this.reverseFlag = !this.reverseFlag;
				if (this.reverseFlag) {
					this.orderBy = 1;
				} else {
					this.orderBy = 2;
				}
				this.chapterList = [];
				this.currentPage = 1;
				this.chapterListScrollTop = this.old.chapterListScrollTop
				this.$nextTick(() => {
					this.chapterListScrollTop = 0;
				});
				this.getChapterList();
			}, 500, {
				'leading': true,
				'trailing': false
			}),
			// 滚动到顶部
			handleToTop() {
				let _scrollTop = 0
				if (this.topFlag) {
					_scrollTop = 10000;
				}
				uni.pageScrollTo({
					duration: 100,
					scrollTop: _scrollTop
				})
			},
			// 去最新章节
			handleToNew() {
				if (!this.bookDetail.new_chapter) return;
				uni.navigateTo({
					url: `/pages/read/index?bookId=${this.bookId}&chapterId=${this.bookDetail.last_chapter_id}`,
				});
			},
			// 开始阅读
			handleToRead() {
				if (this.chapterList.length === 0) {
					this.$refs.uToast.show({
						title: '未找到章节列表,不能阅读该书!',
						type: 'error',
						position: 'top'
					});
					return;
				};
				if (this.isRead) {
					uni.navigateTo({
						url: `/pages/read/index?bookId=${this.bookId}&chapterId=${this.shelfList.find(item => {
						return item.book_id == this.bookId
					}).last_read_chapter}`,
					});
				} else {
					uni.navigateTo({
						url: `/pages/read/index?bookId=${this.bookId}&chapterId=${this.bookDetail.first_chapter_id}`,
					});
				}
			},
			// 跳转指定章节
			handleToCurrentChapter(chapterId) {
				uni.navigateTo({
					url: `/pages/read/index?bookId=${this.bookId}&chapterId=${chapterId}`,
				});
			},
			// 加入or撤出书架
			handleAddBookShelf: _.debounce(async function() {
				if (!this.token) {
					this.$refs.uToast.show({
						title: '您还未登录',
						type: 'error',
						position: 'top'
					})
					return;
				}
				if (this.chapterList.length === 0) {
					this.$refs.uToast.show({
						title: '未找到章节列表,不能加入书架!',
						type: 'error',
						position: 'top'
					});
					return;
				}
				let title = ''
				if (this.isInShelf) {
					const bookShelfId = this.shelfList.find(item => {
						return item.book_id == this.bookId
					}).book_shelf_id
					await this.$store.dispatch('delBookFromShelf', bookShelfId)
					title = '成功撤出书架'
				} else {
					await this.$store.dispatch('addBookToShelf', {
						bookId: this.bookId,
						lastReadChapter: 0,
						index: 1
					})
					title = '成功加入书架'
				}
				await this.$store.dispatch('updateShelfList')
				this.$refs.uToast.show({
					title,
					type: 'success',
					position: 'top'
				})
			}, 500, {
				'leading': true,
				'trailing': false
			})
		}

	}
</script>

<style lang="scss" scoped>
	.detail-box {
		.detail-bg {
			position: fixed;
			top: 0;
			left: 0;
			width: 100vw;
			height: 100vh;
			background-repeat: no-repeat;
			background-size: 100vw 100vh;
			filter: blur(40rpx);
		}

		.detail-wrap {
			position: fixed;
			top: 0;
			left: 0;
			width: 100vw;
			height: 100vh;
			background: linear-gradient(0deg,
				rgba(245, 248, 252, 1) 0%,
				rgba(6, 27, 65, 1) 100%);
			opacity: 0.7;
		}

		.detail-content {
			position: relative;
			display: flex;
			flex-direction: column;
			padding: 60rpx 56rpx;
			color: #fff;

			.detail-top {
				display: flex;
				justify-content: space-between;
				margin-bottom: 48rpx;

				.detail-left {
					color: #fff;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.detail-main {
						color: #fff;

						.detail-name {
							font-size: 36rpx;
							font-family: Alibaba PuHuiTi;
							font-weight: bold;
							color: rgba(255, 255, 255, 1);
							opacity: 0.85;
						}

						.detail-info {
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin-top: 20rpx;

							.detail-writer {
								font-size: 20rpx;
								font-family: Alibaba PuHuiTi;
								font-weight: bold;
								color: rgba(255, 255, 255, 1);
								opacity: 0.85;

							}

							.detail-over {
								font-size: 18rpx;
								font-family: Alibaba PuHuiTi;
								font-weight: bold;
								color: rgba(255, 255, 255, 1);
								line-height: 28rpx;
								opacity: 0.65;
							}
						}
					}

					.detail-new {
						width: 440rpx;
						font-size: 20rpx;
						font-family: Alibaba PuHuiTi;
						font-weight: bold;
						color: rgba(255, 255, 255, 1);
						line-height: 28rpx;
						opacity: 0.85;
						padding: 26rpx 0;
						border-top: 1rpx solid rgba(255, 255, 255, 0.25);
						border-bottom: 1rpx solid rgba(255, 255, 255, 0.25);
						display: flex;
						justify-content: space-between;
						align-items: center;
					}
				}
			}

			.detail-intro {
				font-size: 22rpx;
				font-family: Alibaba PuHuiTi;
				font-weight: 400;
				color: rgba(255, 255, 255, 1);
				line-height: 36rpx;
				opacity: 0.65;
			}
		}

		.detail-list {
			padding: 0 40rpx;

			.list-nav {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 42rpx 40rpx;
				position: relative;
				background: rgba(255, 255, 255, 1);
				border-radius: 23rpx 23rpx 0 0;

				text {
					font-size: 40rpx;
					font-family: Alibaba PuHuiTi;
					font-weight: bold;
					color: rgba(6, 27, 65, 1);
					opacity: 0.75;
				}

				text:last-child {
					font-size: 26rpx;
					font-family: Alibaba PuHuiTi;
					font-weight: 400;
					text-decoration: underline;
					color: rgba(146, 200, 255, 1);
				}
			}

			.list-content {
				background: white;
				position: relative;
				height: 70vh;
				overflow: scroll;
				margin-bottom: 140rpx;
				border-radius: 0 0 23rpx 23rpx;

				.list-item {
					position: relative;
					font-size: 28rpx;
					font-family: Alibaba PuHuiTi;
					font-weight: 400;
					color: #061B41;
					margin: 0 40rpx 0 84rpx;
					padding: 36rpx 0;
					border-bottom: 2rpx solid rgba(6, 27, 65, 0.08);
					display: flex;
					align-items: center;
				}

				.active-item {
					position: relative;
					font-size: 28rpx;
					font-family: Alibaba PuHuiTi;
					font-weight: 400;
					color: #92C8FF;
					margin: 0 40rpx 0 84rpx;
					padding: 36rpx 0;
					border-bottom: 2rpx solid rgba(6, 27, 65, 0.08);
					display: flex;
					align-items: center;
				}

				.read-item {
					position: relative;
					font-size: 28rpx;
					font-family: Alibaba PuHuiTi;
					font-weight: 400;
					color: rgba(6, 27, 65, 0.25);
					margin: 0 40rpx 0 84rpx;
					padding: 36rpx 0;
					border-bottom: 2rpx solid rgba(6, 27, 65, 0.08);
					display: flex;
					align-items: center;
				}

				.list-item::before {
					content: '';
					width: 20rpx;
					height: 20rpx;
					border-radius: 20rpx;
					border: 6rpx solid #061B41;
					position: absolute;
					left: -46rpx;
					top: 44rpx;
				}

				.active-item::before {
					content: '';
					width: 20rpx;
					height: 20rpx;
					border-radius: 20rpx;
					border: 6rpx solid #92C8FF;
					position: absolute;
					left: -46rpx;
					top: 44rpx;
				}
			}
		}

		.detail-bottom {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 100rpx;
			display: flex;
			justify-content: space-between;
			padding: 16rpx 34rpx;
			font-size: 28rpx;
			align-items: center;
			background: rgba(255, 255, 255, 1);
			box-shadow: 0rpx 0rpx 23rpx 0rpx rgba(6, 27, 65, 0.16);
			border-radius: 40rpx 40rpx 0rpx 0rpx;

			.add-shelf {
				width: 50%;
				text-align: center;
			}

			.read-start {
				width: 95%;
				height: 68rpx;
				background: rgba(145, 199, 255, 1);
				border-radius: 50rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: white;
			}
		}
	}
</style>
